import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../views/login.vue'
import Index from '../views/index/index.vue'
import timedata from '../views/index/child/timeData.vue'
import sortData from '../views/index/child/sortData.vue'
import compareData from '../views/index/child/compareData.vue'
import DataList from '../views/index/child/dataList.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/login',
    name: 'Login',
    component: Login
  },
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/index',
    redirect: '/timedata',
    component: Index,
    children: [
      {
        path: '/timeData',
        name: 'timedata',
        component: timedata,
        meta: {
          title: '实时数据'
        }
      },
      {
        path: '/compareData',
        name: 'compareData',
        component: compareData,
        meta: {
          title: '数据对比'
        }
      },
      {
        path: '/DataList',
        name: 'DataList',
        component: DataList,
        meta: {
          title: '数据对比'
        }
      },
      {
        path: '/sortData',
        name: 'sortData',
        component: sortData,
        meta: {
          title: '统计排序'
        }
      }
    ]
  }
]

// const routes = [
//   {
//     path: '/',
//     name: 'Index',
//     redirect: '/index'
//   },
//   {
//     path: '/login',
//     name: 'Login',
//     component: Login
//   },
//   {
//     path: '/login2',
//     name: 'Login2',
//     component: Login2
//   },
//   {
//     path: '/index',
//     redirect: '/timedata', // redirect: '/timeData'
//     component: Index,
//     children: [
//       {
//         path: '/timeData',
//         name: 'timedata',
//         component: timedata,
//         meta: {
//           title: '实时数据'
//         }
//       },
//       {
//         path: '/timeData2',
//         name: 'timedata2',
//         component: timedata2,
//         meta: {
//           title: '历史数据'
//         }
//       },
//       {
//         path: '/compareData',
//         name: 'compareData',
//         component: compareData,
//         meta: {
//           title: '数据对比'
//         }
//       },
//       {
//         path: '/DataList',
//         name: 'DataList',
//         component: DataList,
//         meta: {
//           title: '数据对比'
//         }
//       },
//       {
//         path: '/sortData',
//         name: 'sortData',
//         component: sortData,
//         meta: {
//           title: '统计排序'
//         }
//       }
//     ]
//   }
// ]

const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => {
  if (to.path !== '/login') {
    if (window.localStorage.getItem('token')) {
      next()
    } else {
      router.push('/login')
    }
  } else {
    if (window.localStorage.getItem('token')) {
      router.push('/index')
    } else {
      next()
    }
  }
})

export default router
